import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0',
    'Referer': 'https://www.zhihu.com/'
}

url = "https://www.zhihu.com/billboard"

resp = requests.get(url, headers=headers)
soup = BeautifulSoup(resp.text, "html.parser")

print("知乎热榜")
print("-" * 20)

# 找到包含 JSON 的 script 标签
for script in soup.find_all("script"):
    if "hotList" in script.text:
        import json
        import re

        # 提取 JSON 内容
        json_text = re.search(r'(?<=JSON.parse\(")(.*)(?="\);)', script.text)
        if json_text:
            content = json_text.group(1).encode().decode('unicode_escape')
            data = json.loads(content)
            for i, item in enumerate(data['hotList'], start=1):
                title = item['target']['titleArea']['text']
                url = item['target']['link']['url']
                hot = item['target']['metricsArea']['text']
                print(f"{i}. {title} | 热度：{hot}")
                print(f"   链接：{url}")
        break
